package com.teamb6.shop.controller.front;

import com.github.pagehelper.Page;
import com.teamb6.shop.domain.Collection;
import com.teamb6.shop.domain.Goods;
import com.teamb6.shop.domain.User;
import com.teamb6.shop.service.CollectionService;
import com.teamb6.shop.service.GoodsService;
import com.teamb6.shop.domain.Category;
import com.teamb6.shop.service.CateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Controller
public class MainController {

    @Autowired
    GoodsService goodsService;
    @Autowired
    CateService cateService;
    @Autowired
    CollectionService collectionService;


    //主页面部分商品展示
    @RequestMapping("/main")
    public String showAllGoods(Model model, HttpServletRequest request){
        List<Category> categoryList=cateService.listCategoryAndGoodNumber();
        List<Goods> goodsList = goodsService.getActivityGoods();
        List<Goods> recentGoods=goodsService.getByTime();
        List<Goods> hotGoods=goodsService.getByHot();

        User user = (User) request.getSession().getAttribute("user");


        //轮播图展示3个商品：1个随机活动商品，一个最新上架商品，一个销量最高商品
        Random rand = new Random();
        int max = goodsList.size();
        List<Goods> shufList = new ArrayList<>();
        shufList.add(goodsList.get(rand.nextInt(max)));
        shufList.add(recentGoods.get(0));
        shufList.add(hotGoods.get(0));
        model.addAttribute("shufList",setGoodsListCollection(shufList,user));


        model.addAttribute("categoryList",categoryList);
        model.addAttribute("goodsList",setGoodsListCollection(goodsList,user));
        model.addAttribute("recentGoods",setGoodsListCollection(recentGoods,user));
        model.addAttribute("hotGoods",setGoodsListCollection(hotGoods,user));

        return "index";
    }
    //按热度展示所有商品（销量）
    @RequestMapping("/searchAllByHot")
    public String showAllGoodsByHot(Model model,Integer pageSize, Integer pageNum,Integer pageIndex,HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("user");
        System.out.println(pageSize);
        if(pageSize == null){
            pageSize = 6;
        }
        if(pageNum == null){
            pageNum =1;
        }
        if (pageIndex == null){
            pageIndex =1;
        }

        List<Goods> allHotsGoods=goodsService.getAllByHot(pageSize,pageNum);
        int total = (int) ((Page) allHotsGoods).getTotal();
        int num = total%pageSize == 0? total/pageSize:total/pageSize+1;
        int page[] = new int[num];
        for(int i =0;i<num;i++){
            page[i]=i+1;
        }
        model.addAttribute("allHotsGoods",setGoodsListCollection(allHotsGoods,user));
        model.addAttribute("pages",page);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("pageIndex",pageIndex);
        return "goods-by-Hot";
    }

    //按管理员发布商品时间展示所有商品
    @RequestMapping("/searchAllByTime")
    public String showAllGoodsByTime(Model model,Integer pageSize, Integer pageNum,Integer pageIndex,HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("user");
        if(pageSize == null){
            pageSize = 6;
        }
        if(pageNum == null){
            pageNum =1;
        }
        if (pageIndex == null){
            pageIndex =1;
        }
        List<Goods> allRencentGoods=goodsService.getAllByTime(pageSize,pageNum);
        int total = (int) ((Page) allRencentGoods).getTotal();
        int num = total%pageSize == 0? total/pageSize:total/pageSize+1;
        int page[] = new int[num];
        for(int i =0;i<num;i++){
            page[i]=i+1;
        }
        model.addAttribute("allRencentGoods",setGoodsListCollection(allRencentGoods,user));
        model.addAttribute("pages",page);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("pageIndex",pageIndex);
        return "goods-by-Time";
    }

    //展示所有有活动的商品
    @RequestMapping("/searchAllByActivity")
    public String showAllGoodsByActivity(Model model,Integer pageSize, Integer pageNum,Integer pageIndex,HttpServletRequest request){

        User user= (User) request.getSession().getAttribute("user");

        if(pageSize == null){
            pageSize = 6;
        }
        if(pageNum == null){
            pageNum =1;
        }
        if (pageIndex == null){
            pageIndex =1;
        }
        List<Goods> goodsList = goodsService.selectAllByActivity(pageSize,pageNum);
        int total = (int) ((Page) goodsList).getTotal();
        int num = total%pageSize == 0? total/pageSize:total/pageSize+1;
        int page[] = new int[num];
        for(int i =0;i<num;i++){
            page[i]=i+1;
        }
        model.addAttribute("goodsList",setGoodsListCollection(goodsList,user));
        model.addAttribute("pages",page);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("pageIndex",pageIndex);
        return "goods-by-activity";
    }


    //按关键字模糊查询商品
    @RequestMapping("/search")
    public String searchGoods(Model model,String goodsName,Integer pageSize, Integer pageNum,Integer pageIndex,HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("user");
        if (pageSize==null){
            pageSize=6;
        }
        if (pageNum==null){
            pageNum=1;
        }
        if (pageIndex==null){
            pageIndex=1;
        }

        List<Goods> goodsList = goodsService.mySelectByName(goodsName,pageSize,pageNum);
        int total = (int) ((Page) goodsList).getTotal();
        int num = total%pageSize == 0? total/pageSize:total/pageSize+1;
        int page[] = new int[num];
        for(int i =0;i<num;i++){
            page[i]=i+1;
        }
        List<Category> categoryList=cateService.listCategoryAndGoodNumber();
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("goodsList",setGoodsListCollection(goodsList,user));
        model.addAttribute("pages",page);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("pageIndex",pageIndex);
        return "deals_grid";
    }

    //展示所有商品
    @RequestMapping("/jump")
    public String junmp(Model model,Integer pageSize, Integer pageNum,Integer pageIndex,HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("user");
        if (pageSize==null){
            pageSize=6;
        }
        if (pageNum==null){
            pageNum=1;
        }
        if (pageIndex==null){
            pageIndex=1;
        }


        List<Goods> goodsList = goodsService.findAllProducts(pageSize,pageNum);
        int total = (int) ((Page) goodsList).getTotal();
        int num = total%pageSize == 0? total/pageSize:total/pageSize+1;
        int page[] = new int[num];
        for(int i =0;i<num;i++){
            page[i]=i+1;
        }
        List<Category> categoryList=cateService.listCategoryAndGoodNumber();
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("goodsList",setGoodsListCollection(goodsList,user));
        model.addAttribute("pages",page);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("pageIndex",pageIndex);
        return "deals_grid";
    }




    //判断当前商品是否被当前用户收藏
    public Goods setGoodsCollection(Goods goods, User user){
        if(user==null) {
            goods.setCollection(false);
        }else {
            Collection collection=collectionService.getByCollectionKey(user.getUserid(),goods.getGoodsid());//
            if(collection==null) {
                goods.setCollection(false);
            }else {
                goods.setCollection(true);
            }
        }
        return goods;
    }
    //判断商品List中所有商品是否被当前用户收藏
    public List<Goods> setGoodsListCollection(List<Goods> goodsList, User user){
        for (Goods goods:goodsList
             ) {
            setGoodsCollection(goods,user);
        }
        return goodsList;
    }

    //按热度展示所有商品（销量）价格从低到高
    @RequestMapping("/showHotGoodsByLowPrice")
    public String showAllGoodsByLowPrice(Model model,Integer pageSize, Integer pageNum,Integer pageIndex){

        System.out.println(pageSize);
        if(pageSize == null){
            pageSize = 6;
        }
        if(pageNum == null){
            pageNum =1;
        }
        if (pageIndex == null){
            pageIndex =1;
        }

        List<Goods> allHotsGoods=goodsService.getHotByLowPrice(pageSize,pageNum);
        int total = (int) ((Page) allHotsGoods).getTotal();
        int num = total%pageSize == 0? total/pageSize:total/pageSize+1;
        int page[] = new int[num];
        for(int i =0;i<num;i++){
            page[i]=i+1;
        }
        List<Category> categoryList=cateService.listCategoryAndGoodNumber();
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("allHotsGoods",allHotsGoods);
        model.addAttribute("pages",page);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("pageIndex",pageIndex);
        return "goods-by-Hot";
    }

    //按热度展示所有商品（销量）价格从低到高
    @RequestMapping("/showHotGoodsByHighPrice")
    public String showAllGoodsByHighPrice(Model model,Integer pageSize, Integer pageNum,Integer pageIndex){

        System.out.println(pageSize);
        if(pageSize == null){
            pageSize = 6;
        }
        if(pageNum == null){
            pageNum =1;
        }
        if (pageIndex == null){
            pageIndex =1;
        }

        List<Goods> allHotsGoods=goodsService.getHotByHighPrice(pageSize,pageNum);
        int total = (int) ((Page) allHotsGoods).getTotal();
        int num = total%pageSize == 0? total/pageSize:total/pageSize+1;
        int page[] = new int[num];
        for(int i =0;i<num;i++){
            page[i]=i+1;
        }
        List<Category> categoryList=cateService.listCategoryAndGoodNumber();
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("allHotsGoods",allHotsGoods);
        model.addAttribute("pages",page);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("pageIndex",pageIndex);
        return "goods-by-Hot";
    }
//------------------------------------------------------------------------------------
        //按活动展示所有商品（销量）价格从低到高
        @RequestMapping("/showActivityGoodsByLowPrice")
        public String showActivityGoodsByLowPrice(Model model,Integer pageSize, Integer pageNum,Integer pageIndex){

            System.out.println(pageSize);
            if(pageSize == null){
                pageSize = 6;
            }
            if(pageNum == null){
                pageNum =1;
            }
            if (pageIndex == null){
                pageIndex =1;
            }

            List<Goods> goodsList=goodsService.getActivityByLowPrice(pageSize,pageNum);
            int total = (int) ((Page) goodsList).getTotal();
            int num = total%pageSize == 0? total/pageSize:total/pageSize+1;
            int page[] = new int[num];
            for(int i =0;i<num;i++){
                page[i]=i+1;
            }
            List<Category> categoryList=cateService.listCategoryAndGoodNumber();
            model.addAttribute("categoryList",categoryList);
            model.addAttribute("goodsList",goodsList);
            model.addAttribute("pages",page);
            model.addAttribute("pageSize",pageSize);
            model.addAttribute("pageIndex",pageIndex);
            return "goods-by-Activity";
        }

            //按活动展示所有商品（销量）价格从低到高
            @RequestMapping("/showActivityGoodsByHighPrice")
            public String showActivityGoodsByHighPrice(Model model,Integer pageSize, Integer pageNum,Integer pageIndex){

                System.out.println(pageSize);
                if(pageSize == null){
                    pageSize = 6;
                }
                if(pageNum == null){
                    pageNum =1;
                }
                if (pageIndex == null){
                    pageIndex =1;
                }

                List<Goods> goodsList=goodsService.getActivityByHighPrice(pageSize,pageNum);
                int total = (int) ((Page) goodsList).getTotal();
                int num = total%pageSize == 0? total/pageSize:total/pageSize+1;
                int page[] = new int[num];
                for(int i =0;i<num;i++){
                    page[i]=i+1;
                }

                List<Category> categoryList=cateService.listCategoryAndGoodNumber();
                model.addAttribute("categoryList",categoryList);
                model.addAttribute("goodsList",goodsList);
                model.addAttribute("pages",page);
                model.addAttribute("pageSize",pageSize);
                model.addAttribute("pageIndex",pageIndex);
                return "goods-by-Activity";
            }


    //------------------------------------------------------------------------------------
    //展示所有商品（销量）价格从低到高
    @RequestMapping("/showTimeGoodsByLowPrice")
    public String showTimeGoodsByLowPrice(Model model,Integer pageSize, Integer pageNum,Integer pageIndex){

        System.out.println(pageSize);
        if(pageSize == null){
            pageSize = 6;
        }
        if(pageNum == null){
            pageNum =1;
        }
        if (pageIndex == null){
            pageIndex =1;
        }

        List<Goods> allRencentGoods=goodsService.getAllByTime(pageSize,pageNum);
        int total = (int) ((Page) allRencentGoods).getTotal();
        int num = total%pageSize == 0? total/pageSize:total/pageSize+1;
        int page[] = new int[num];
        for(int i =0;i<num;i++){
            page[i]=i+1;
        }
        List<Category> categoryList=cateService.listCategoryAndGoodNumber();
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("allRencentGoods",allRencentGoods);
        model.addAttribute("pages",page);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("pageIndex",pageIndex);
        return "goods-by-Time";
    }

    //展示所有商品（销量）价格从低到高
    @RequestMapping("/showTimeGoodsByHighPrice")
    public String showTimeGoodsByHighPrice(Model model,Integer pageSize, Integer pageNum,Integer pageIndex){

        System.out.println(pageSize);
        if(pageSize == null){
            pageSize = 6;
        }
        if(pageNum == null){
            pageNum =1;
        }
        if (pageIndex == null){
            pageIndex =1;
        }

        List<Goods> allRencentGoods=goodsService.getAllByTime(pageSize,pageNum);
        int total = (int) ((Page) allRencentGoods).getTotal();
        int num = total%pageSize == 0? total/pageSize:total/pageSize+1;
        int page[] = new int[num];
        for(int i =0;i<num;i++){
            page[i]=i+1;
        }

        List<Category> categoryList=cateService.listCategoryAndGoodNumber();
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("allRencentGoods",allRencentGoods);
        model.addAttribute("pages",page);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("pageIndex",pageIndex);
        return "goods-by-Time";
    }

    //------------------------------------------------------------------------------------
    //分类查询
    @RequestMapping("/showCateGoodsByLowPrice")
    public String showCateGoodsByLowPrice(Integer categoryId, Model model,Integer pageNum, Integer pageSize,Integer pageIndex){
        if (pageSize==null){
            pageSize=6;
        }
        if (pageNum==null){
            pageNum=1;
        }
        if (pageIndex == null){
            pageIndex =1;
        }

        List<Goods> goodsList = goodsService.getByCategoryIdLow(categoryId,pageNum,pageSize);
        int total = (int) ((Page) goodsList).getTotal();
        int num = total%pageSize == 0? total/pageSize:total/pageSize+1;
        int pages[] = new int[num];
        for(int i =0;i<num;i++){
            pages[i]=i+1;
        }

        List<Category> categoryList=cateService.listCategoryAndGoodNumber();
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("categoryId",categoryId);
        model.addAttribute("goodsList",goodsList);
        model.addAttribute("pages",pages);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("pageIndex",pageIndex);
        return "goods-by-category";
    }

    //分类查询
    @RequestMapping("/showCateGoodsByHighPrice")
    public String showCateGoodsByHighPrice(Integer categoryId, Model model,Integer pageNum, Integer pageSize,Integer pageIndex){
        if (pageSize==null){
            pageSize=6;
        }
        if (pageNum==null){
            pageNum=1;
        }
        if (pageIndex == null){
            pageIndex =1;
        }

        List<Goods> goodsList = goodsService.getByCategoryIdHigh(categoryId,pageNum,pageSize);
        int total = (int) ((Page) goodsList).getTotal();
        int num = total%pageSize == 0? total/pageSize:total/pageSize+1;
        int pages[] = new int[num];
        for(int i =0;i<num;i++){
            pages[i]=i+1;
        }

        List<Category> categoryList=cateService.listCategoryAndGoodNumber();
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("categoryId",categoryId);
        model.addAttribute("goodsList",goodsList);
        model.addAttribute("pages",pages);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("pageIndex",pageIndex);
        return "goods-by-category";
    }

    //-----------------------------------------------------------------------------------------------------------------------
    //------------------------------------------------------------------------------------
    //所有商品按价格分类查询
    @RequestMapping("/showAllsGoodsByLowPrice")
    public String showAllsGoodsByLowPrice(Model model,Integer pageSize, Integer pageNum,Integer pageIndex){

        System.out.println(pageSize);
        if(pageSize == null){
            pageSize = 6;
        }
        if(pageNum == null){
            pageNum =1;
        }
        if (pageIndex == null){
            pageIndex =1;
        }

        List<Goods> goodsList=goodsService.getAllsByLowPrice(pageSize,pageNum);
        int total = (int) ((Page) goodsList).getTotal();
        int num = total%pageSize == 0? total/pageSize:total/pageSize+1;
        int page[] = new int[num];
        for(int i =0;i<num;i++){
            page[i]=i+1;
        }
        List<Category> categoryList=cateService.listCategoryAndGoodNumber();
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("goodsList",goodsList);
        model.addAttribute("pages",page);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("pageIndex",pageIndex);
        return "deals_grid";
    }

    //所有商品按价格分类查询
    @RequestMapping("/showAllsGoodsByHighPrice")
    public String showAllsGoodsByHighPrice(Model model,Integer pageSize, Integer pageNum,Integer pageIndex){

        System.out.println(pageSize);
        if(pageSize == null){
            pageSize = 6;
        }
        if(pageNum == null){
            pageNum =1;
        }
        if (pageIndex == null){
            pageIndex =1;
        }

        List<Goods> goodsList=goodsService.getAllsByHighPrice(pageSize,pageNum);
        int total = (int) ((Page) goodsList).getTotal();
        int num = total%pageSize == 0? total/pageSize:total/pageSize+1;
        int page[] = new int[num];
        for(int i =0;i<num;i++){
            page[i]=i+1;
        }
        List<Category> categoryList=cateService.listCategoryAndGoodNumber();
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("goodsList",goodsList);
        model.addAttribute("pages",page);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("pageIndex",pageIndex);
        return "deals_grid";
    }
        }

